e = []
p = int(input())
c = []
d = True
p1 = p // 1000
p2 = p // 100
p3 = p // 10
p4 = p // 100 % 10
p5 = p // 10 % 100
p6 = p % 100
p7 = p % 1000
p8 = p // 10 % 10
p9 = p % 10


def is_prime(a):
    b = 2
    while a % b != 0:
        b += 1
    return b == a


for i in range(2, 998):
    if is_prime(i) == d:
        e.append(i)
if len(str(p)) != 4:
    print(0)
    exit()
if p1 in e:
    c.append(p1)
if p2 in e:
    c.append(p2)
if p3 in e:
    c.append(p3)
if p4 in e:
    c.append(p4)
if p5 in e:
    c.append(p5)
if p6 in e:
    c.append(p6)
if p7 in e:
    c.append(p7)
if p8 in e:
    c.append(p8)
if p9 in e:
    c.append(p9)
if is_prime(p) == d:
    c.append(p)
if len(c) == 0:
    print(0)
else:
    print(max(c))